package com.gxa.clientproduct.service.impl;


import com.gxa.clientproduct.mapper.UserMapper;
import com.gxa.clientproduct.pojo.User;
import com.gxa.clientproduct.service.UserService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import java.util.List;

/**
 * @Author: fenglin
 * @Date:2020/11/12 14:40
 * @Version 1.0
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Override
    public List<User> findNameAndPhone(String name, String phone) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (name!=null){
            criteria.andLike("username","%"+name+"%");
        }
        if (phone!=null){
            criteria.andLike("phone","%"+phone+"%");
        }

        return userMapper.selectByExample(example);
    }

    @Override
    public List<User> findAddress(String phone) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",phone);

        return userMapper.selectByExample(example);
    }

    @Override
    public int delete(User user) {

        return userMapper.delete(user);
    }

    @Override
    public int add(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public int updateUser(User user) {

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",user.getPhone());

        return userMapper.updateByExampleSelective(user,example);
    }

    @Override
    public User findPhone(String phone) {

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",phone);
        return userMapper.selectOneByExample(example);
    }
}
